package net.esj.background.struts2.view;

import net.esj.auth.pojo.CoreAdmin;
import net.esj.background.struts2.view.easyui.AbstractBgAction;
import net.esj.basic.pojo.system.UserRule;
import net.esj.basic.service.provider.UserRuleProvider;

public class LoginAction extends AbstractBgAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1495125202956771748L;

	private String  captcha;
	
	private String name;
	
	private String pwd;
	
	private String newpwd;
	
	private String hospital;
	
	private String loginname;
	
	private String rpwd;
	
	private String id;
	private String df;
	private String path;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String doLogin(){
		Object user = null;
		if(name!=null && name.equals(CoreAdmin.SUPER_ADMINISTRATOR_NAME)){
			user = loginManage.superLoing(name, pwd,captcha);
		}else{
			user = loginManage.doLoginByNameAndPwd(name, pwd,captcha);
		}
		if(portal.getTimeout()!=0){
			getSession().setMaxInactiveInterval((int) (portal.getTimeout()*60));
		}
		if(user==null){
			return INPUT;
		}
		return SUCCESS;
	}
	
	public void doAjaxLogin(){
		writeJson(doLogin(),"html");
	}
	
	public void doAgencyRegister(){
		String result = SUCCESS;
		Object user = loginManage.doLoginByNameAndPwd(loginname, rpwd,captcha);
		if(user==null){
			result = INPUT;
		}
		if(portal.getTimeout()!=0){
			getSession().setMaxInactiveInterval((int) (portal.getTimeout()*60));
		}
		writeJson(result,"html");
	}
	
	public String doLogout(){
		loginManage.logout();
		return INPUT;
	}
	
	public void doAjaxLogout(){
		loginManage.logout();
	}
	
	public String download(){
		return SUCCESS;
	}
	
	public String alterPwd(){
		if(loginManage.alterPwd(pwd, newpwd, captcha)){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}
	
	public void doAjaxAlterPwd(){
		writeJson(alterPwd(), "html");
	}
	
	protected String getSessionKey(Class clazz){
		UserRule rule = UserRuleProvider.getUserRule(clazz);
		if(rule!=null){
			return rule.getSessionKey();
		}
		return null;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public String getHospital() {
		return hospital;
	}

	public void setHospital(String hospital) {
		this.hospital = hospital;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getRpwd() {
		return rpwd;
	}

	public void setRpwd(String rpwd) {
		this.rpwd = rpwd;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getDf() {
		return df;
	}

	public void setDf(String df) {
		this.df = df;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
}
